#include "widget.h"
#include "ui_widget.h"
#include<QFile>
#include<QFileDialog>

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
}

Widget::~Widget()
{
    delete ui;
}

void Widget::on_buttonRead_clicked()
{
   QString  path = QFileDialog::getOpenFileName(this,"open","../");
   if(path.isEmpty() == false)
   {
       QFile file(path);
       bool isOK = file.open(QIODevice::ReadOnly);
       if(isOK == true)
       {
           //读文件,默认只支持utf8编码
           QByteArray array = file.readAll();
           ui->textEdit->setText(array);

       }

       file.close();
   }
}

void Widget::on_pushButton_2_clicked()
{
    QString path = QFileDialog::getSaveFileName(this,"save","../");
    if(path.isEmpty() == false)
    {
        QFile file;//创建文件对象
        file.setFileName(path);//关联文件名字
        //打开文件，只写方式并判断打开是否成功
        bool isOK =file.open(QIODevice::WriteOnly);
        if(isOK == true)
        {
            QString str = ui->textEdit->toPlainText();
            file.write(str.toUtf8());
        }

        file.close();
    }

}
